package com.hainatong.shiphelper.mainhome.fragment;

import java.util.ArrayList;
import java.util.List;

import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentRecyclerviewBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.adapter.MessageAdapter;
import com.hainatong.shiphelper.mainhome.entity.MsgBean;
import com.hainatong.shiphelper.mainhome.viewmodel.MsgViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;

public class MessageListFragment extends MvvmLazyFragment<FragmentRecyclerviewBinding , MsgViewModel>
{

    private MessageAdapter messageAdapter;
    private List<MsgBean> messageList;
    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        messageList = new ArrayList<>();
        messageAdapter = new MessageAdapter();
        viewDataBinding.recyclerview.setAdapter( messageAdapter );
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.setLayoutManager( new LinearLayoutManager( getActivity() ));

        viewDataBinding.refreshlayout.setRefreshHeader( new ClassicsHeader(getActivity()));
        viewDataBinding.refreshlayout.setRefreshFooter( new ClassicsFooter( getActivity() ));
        viewDataBinding.refreshlayout.setOnRefreshListener( refreshLayout -> {
            refreshLayout.finishRefresh(true);
        } );
        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {
            refreshLayout.finishLoadMore( true );
        } );


        messageAdapter.setOnItemClickListener( (adapter, view1, position) -> {
            MsgBean mMsg = messageAdapter.getItem( position );
            ComFragmentActivity.startViewByParam( getActivity(), Constants.VIEW_WARN_MSG_DEATIL , GsonUtil.obj2json( mMsg ));
        } );
    }

    @Override
    protected void lazyLoad()
    {

        viewModel.queryMsgList();

        viewModel.getMsgListResult().observe( this , result -> {

            if( result.isSuccess() )
            {
                if( result.getData() != null )
                {
                    messageList = result.getData();
                    messageAdapter.setList( messageList );
                }
            }
            else
            {

            }

        } );

    }
}
